using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using System.Diagnostics;
using UselessExplorer.Properties;


namespace UselessExplorer
{
    public partial class frmSearchReplace : Form
    {
        public frmSearchReplace()
        {
            InitializeComponent();
        }



        private void textBox1_Click(object sender, EventArgs e)
        {
            //folderBrowserDialog1.ShowDialog();
            // string path = folderBrowserDialog1.SelectedPath.ToString();
            // textBox1.Text = path;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();

            DirectoryInfo di = new DirectoryInfo(textBox1.Text);
            string selectedExt = null;
            selectedExt = "*" + comboBox1.SelectedItem.ToString();

            if (selectedExt == "All")
                selectedExt = "*.*";

            if (selectedExt != "")
            {
                FileInfo[] rgFiles = di.GetFiles(selectedExt);

                string searchText = "";
                string replaceText = "";
                foreach (FileInfo fi in rgFiles)
                {
                    string filePath = textBox1.Text + @"\" + fi.Name;
                    searchText = richTextBox1.Text; // +Environment.NewLine;
                    replaceText = richTextBox2.Text;

                    StreamReader reader = new StreamReader(filePath);
                    string content = reader.ReadToEnd();
                    reader.Close();

                    //bool ans = Regex.IsMatch(, searchText, RegexOptions.ExplicitCapture);
                    
                    listBox1.Items.Add(fi.Name);
                    //else
                    //    listBox2.Items.Add(fi.Name);
                }
                if (DialogResult.Yes == MessageBox.Show("Replacing will Start. Do you wish to continue?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                {
                    string Args = searchText + "~" + replaceText;
                    backgroundWorker1.RunWorkerAsync(Args);
                }

            }
            else
            {
                MessageBox.Show("Please Select File Extension value");
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            System.ComponentModel.BackgroundWorker worker = (System.ComponentModel.BackgroundWorker)sender;
            string[] args = new string[2];
            args = ((string)e.Argument).Split('~');
            do_work(args[0],args[1]);
        }

        public void do_work(string text, string text2)
        {

            foreach (string item in listBox1.Items)
            {
                string filePath = textBox1.Text + "\\" + item;
                string searchText = text;
                string replaceText = text2;
                Debug.Print (text + text2);
                StreamReader reader = new StreamReader(filePath);
                string content = reader.ReadToEnd();
                reader.Close();
                // content = Regex.Replace(content, searchText, replaceText, RegexOptions.None);
                content = content.Replace(searchText, replaceText);
                StreamWriter writer = new StreamWriter(filePath);
                writer.Write(content);
                writer.Close();
                //listBox2.Items.Add(item);
                //listBox1.Items.Remove(item);
            }

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Task Completed");
            toolStripStatusLabel1.Text = "Task Completed";
            timer1.Enabled = true;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string filePath = textBox1.Text + @"\" + listBox1.SelectedItem.ToString();
            frmTxtViewer ftv = new frmTxtViewer(filePath);
            ftv.Show();
        }

        private void frmSearchReplace_Load(object sender, EventArgs e)
        {
            this.Text = UselessResources.frmSrchRepTitle;
            this.toolStripStatusLabel1.Text = "";
            //string[] extensions = frmAllowedExtensions.readConfiguration();
            clsGlobal cg = clsGlobal.Instance;

            string[] extensions = cg.Extenstions;

            comboBox1.Items.Add("All");
            comboBox1.Items.Add(".html");
            comboBox1.SelectedItem = "All";
            foreach (string extension in extensions)
            {
                comboBox1.Items.Add(extension);
            }

        }

        private void richTextBox1_Click(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = richTextBox1;
            this.propertyGrid1.Visible = true;
            this.label4.Visible = true;
        }

        private void richTextBox2_Click(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = richTextBox2;
            this.propertyGrid1.Visible = true;
            this.label4.Visible = true;
        }

        private void richTextBox1_Leave(object sender, EventArgs e)
        {
            this.propertyGrid1.Visible = false;
            this.label4.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "";
            timer1.Enabled = false;
        }

       

    }
}